package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.model.request.DrillRequest;
import com.fin.zw.aiqas.model.request.TemplateInfoRequest;
import com.fin.zw.aiqas.model.request.TemplateQueryRequest;
import com.fin.zw.aiqas.model.response.TemplateInfoResponse;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.TemplateInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/templateInfo")
@Api(value = "样本信息管理", description = "样本信息管理")
public class TemplateInfoController {

    @Autowired
    private TemplateInfoService templateInfoService;


    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询样本信息", notes = "查询样本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<TemplateInfoResponse>> queryRuleClassify(TemplateQueryRequest request, Pageable pageable) {
        Page<TemplateInfoResponse> page = templateInfoService.queryTemplateInfo(request,pageable);
        return ResponseEntity.ok().body(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建样本信息", notes = "创建样本信息")
    public ResponseEntity<String> createRuleClassify(@RequestBody TemplateInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel) {
        templateInfoService.createInfo(request, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "样本信息修改", notes = "样本信息修改")
    public ResponseEntity<String> editRuleClassify(@RequestBody TemplateInfoRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel) {
        templateInfoService.updateInfo(request, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "样本信息删除", notes = "样本信息删除")
    public ResponseEntity<String> deleteRuleClassify(@PathVariable Long id) {
        templateInfoService.deleteInfo(id);
        return ResponseEntity.ok().body("删除成功");
    }

    @PostMapping("/exportTemplate")
    @ApiOperation(value = "导出质检点样本配置", notes = "导出质检点样本配置")
    public ResponseEntity<Void> exportTemplate(HttpServletResponse response) {
        templateInfoService.exportTemplate(response);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/importTemplate")
    @ApiOperation(value = "导入质检点样本配置", notes = "导入质检点样本配置")
    public ResponseEntity<Void> importTemplate(@RequestParam("fileId") Long fileId,
                                               @ApiIgnore @CurrentUser SessionModel sessionModel) throws Exception {
        templateInfoService.importTemplateInfo(fileId, sessionModel);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/drillFormula")
    @ApiOperation(value = "样本的训练", notes = "样本的训练")
    public ResponseEntity<Void> drillFormula(@RequestBody DrillRequest request) throws Exception {
        templateInfoService.drillBySceId(request.getId());
        return ResponseEntity.ok().build();
    }

    @RequestMapping(value = "/updateSceState", method = {RequestMethod.POST, RequestMethod.PATCH})
    @ApiOperation(value = "样本训练后更新对应场景下的状态", notes = "样本训练后更新对应场景下的状态")
    public ResponseEntity<Void> updateSceState(@RequestBody DrillRequest request) {
        templateInfoService.updateSceState(request);
        return ResponseEntity.ok().build();
    }

}
